﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ObviousCode.Aspect.PluginLibrary.Interfaces;
using ObviousCode.Aspect.PluginLibrary.Themes;
using ObviousCode.Common.Utils.Library.Exceptions;
using ObviousCode.Common.Utils.Library;

namespace ObviousCode.Aspect.PluginLibrary.Descriptors
{
    public class SurfaceRelationshipDescriptorStore
    {
        Dictionary<string, SurfaceRelationshipDescriptor> _store;

        public SurfaceRelationshipDescriptorStore()
        {
            _store = new Dictionary<string, SurfaceRelationshipDescriptor>();
        }

        public SurfaceRelationshipDescriptor AddDescriptor(string key, Func<Pair<IAspectItem, IAspectItem>, object[], IAspectRelationship> builder)
        {
            return AddDescriptor(key, key, new BasicRelationshipTheme(), builder);
        }

        public SurfaceRelationshipDescriptor AddDescriptor(string key, string description, Func<Pair<IAspectItem, IAspectItem>, object[], IAspectRelationship> builder)
        {
            return AddDescriptor(key, description, new BasicRelationshipTheme(), builder);
        }

        public SurfaceRelationshipDescriptor AddDescriptor(string key, string description, IUIRelationshipTheme theme, Func<Pair<IAspectItem, IAspectItem>, object[], IAspectRelationship> builder)
        {
            SurfaceRelationshipDescriptor descriptor = new SurfaceRelationshipDescriptor(key, description, theme, builder);
            _store[key] = descriptor;

            return descriptor;
        }

        public IEnumerable<string> Keys
        {
            get
            {
                return _store.Keys;
            }
        }

        public SurfaceRelationshipDescriptor this[string key]
        {
            get
            {
                if (!_store.ContainsKey(key)) ExceptionBuilder.Format("Descriptor Store does not contain key \"{0}\"", key);

                return _store[key];
            }    
            set
            {
                if (value == null || string.IsNullOrEmpty(key)) new InvalidOperationException();
                _store[key] = value;
            }
        }

        internal bool ContainsKey(string key)
        {
            return _store.ContainsKey(key);
        }
    }
}
